﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using customer.dao.V2;
using Nancy;
using Nancy.ModelBinding;

namespace customer.dao
{
    public class ODataModule : NancyModule
    {
        
        private DBEngineBuilder _dbEngineBuilder = null;

        public ODataModule(DBEngineBuilder dbEngineBuilder)
        {
            _dbEngineBuilder = dbEngineBuilder;

            this.Post("/odata/{action}", x => DoResponse(x));

            this.Post("/odata/query/{pagesize}/{pageindex}/{pk}", x => query(x));

            this.Before.AddItemToStartOfPipeline(ctx => {

                if (ctx.Request.Path == "")
                {
                    var s = "as";
                }
                else
                {
                    var p = ctx.Request.Path;
                }

                return null;
            });
        }


        dynamic DoResponse(dynamic x)
        {
            string action = x.action;

            if (action == "write")
                return write(x);
            else if (action == "scale")
                return scale(x);
            else if (action == "get")
                return get(x);
            else if (action == "test")
                return test(x);

            return null;
        }

        /// <summary>
        /// 查询操作
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        dynamic query(dynamic x)
        {
            int pagesize = x.pagesize;
            int pageindex = x.pageindex;
            string pk = x.pk;

            var qm = this.Bind<SqlModel>();

            QueryResult result = _dbEngineBuilder.GetDB(qm.DB).Query(qm.Sql, pk, pageindex, pagesize);

            return this.Response.AsText(result.ToString(), "application/json");
        }

        /// <summary>
        /// 写入操作
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        dynamic write(dynamic x)
        {
            var sm = this.Bind<SqlModel>();

            WriteResult result = _dbEngineBuilder.GetDB(sm.DB).Write(sm.Sql);

            return this.Response.AsText(result.ToString(), "application/json");
        }
        /// <summary>
        /// 读取标量
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        dynamic scale(dynamic x)
        {
            var sm = this.Bind<SqlModel>();

            ScaleResult result = _dbEngineBuilder.GetDB(sm.DB).Scale(sm.Sql);

            return this.Response.AsText(result.ToString(), "application/json");
        }
        /// <summary>
        /// 查询一条记录
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        dynamic get(dynamic x)
        {
            var sm = this.Bind<SqlModel>();

            GetResult result = _dbEngineBuilder.GetDB(sm.DB).Get(sm.Sql);

            return this.Response.AsText(result.ToString(), "application/json");
        }

        dynamic test(dynamic x)
        {
            var sm = this.Bind<SqlModel>();

            DataResult result = _dbEngineBuilder.GetDB(sm.DB).Test();

            return this.Response.AsText(result.ToString(), "application/json");
        }

    }
}
